//------------------------------------------------------------------------------
// <copyright company="Telligent Systems">
//     Copyright (c) Telligent Systems Corporation.  All rights reserved.
// </copyright> 
//------------------------------------------------------------------------------

using System;
using CommunityServer.Components;
using CommunityServer.ControlPanel.UI;
using CommunityServer.Galleries.Components;
using CA = ComponentArt.Web.UI;

namespace CommunityServer.ControlPanel.PhotoAdmin
{
	/// <summary>
	///		Summary description for DefaultPermissionListControl.
	/// </summary>
	public class DefaultPermissionListControl : BaseGalleryPermissionsGridControl
	{

		private void Page_Load(object sender, EventArgs e)
		{
			this.SectionID = -1;
		}

		#region Web Form Designer generated code
		override protected void OnInit(EventArgs e)
		{
			//
			// CODEGEN: This call is required by the ASP.NET Web Form Designer.
			//
			InitializeComponent();
			base.OnInit(e);
			this.Grid.DeleteCommand += new CA.Grid.GridItemEventHandler(this.Grid_DeleteCommand);
		}
		
		/// <summary>
		///		Required method for Designer support - do not modify
		///		the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
			this.Load += new EventHandler(this.Page_Load);

		}
		#endregion

		protected override bool CreateFromDefault(PermissionBase permission)
		{
			return false;
		}

		protected override bool IsNew(PermissionBase permission)
		{
			return permission == null;
		}

		/// <summary>
		/// We are using this event to reset permissions to default for a role
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void Grid_DeleteCommand(object sender, CA.GridItemEventArgs e)
		{
			SetDefaultPermissions(Roles.GetRole(new Guid(e.Item["RoleIDString"].ToString())));
		}

		public void ResetPermissions()
		{
			if(!CSContext.Current.User.IsAdministrator)
				throw new CSException(CSExceptionType.AdministrationAccessDenied);

			foreach(Role r in Roles.GetRoles())
				SetDefaultPermissions(r);

			//Seems like a CA Bug, but our checkboxes will loose their values if we postback without re-binding the data
			buildGrid();
			Grid.DataBind();


		}

		private void SetDefaultPermissions(Role r)
		{
			GalleryPermission p = null;
			bool isNew = false;
			p = GalleryPermissions.GetGalleryPermission(-1, r.RoleID);
					
			if(p == null)
			{
				isNew = true;
				p = new GalleryPermission();
				p.SectionID = -1;
				p.RoleID = r.RoleID;

			}

			//Reset the permissions
			p.ApplicationType = ApplicationType.Gallery;
			p.AllowMask = Permission.Undefined;
			p.DenyMask =  Permission.Undefined;

			if(r.Name == Roles.BlogAdministrator)
			{
				p.SetBit(Permission.SystemAdmin, AccessControlEntry.Deny);
				p.SetBit(Permission.Administer, AccessControlEntry.Allow);
				p.SetBit(Permission.Moderate, AccessControlEntry.Allow);
				p.SetBit(Permission.Vote, AccessControlEntry.Allow);
				p.SetBit(Permission.Delete, AccessControlEntry.Allow);
				p.SetBit(Permission.Edit, AccessControlEntry.Allow);
				p.SetBit(Permission.Reply, AccessControlEntry.Allow);
				p.SetBit(Permission.Post, AccessControlEntry.Allow);
				p.SetBit(Permission.View, AccessControlEntry.Allow);
				p.SetBit(Permission.RemoteAttachment, AccessControlEntry.Allow);
			}
			else if(r.Name == Roles.Editor)
			{
				p.SetBit(Permission.SystemAdmin, AccessControlEntry.Deny);
				p.SetBit(Permission.Administer, AccessControlEntry.Deny);
				p.SetBit(Permission.Moderate, AccessControlEntry.Deny);
				p.SetBit(Permission.Vote, AccessControlEntry.Allow);
				p.SetBit(Permission.Delete, AccessControlEntry.Allow);
				p.SetBit(Permission.Edit, AccessControlEntry.Allow);
				p.SetBit(Permission.Reply, AccessControlEntry.Allow);
				p.SetBit(Permission.Post, AccessControlEntry.Allow);
				p.SetBit(Permission.View, AccessControlEntry.Allow);
				p.SetBit(Permission.RemoteAttachment, AccessControlEntry.Allow);
			}
			else if(r.Name == Roles.Everyone)
			{
				p.SetBit(Permission.SystemAdmin, AccessControlEntry.Deny);
				p.SetBit(Permission.Administer, AccessControlEntry.Deny);
				p.SetBit(Permission.Moderate, AccessControlEntry.Deny);
				p.SetBit(Permission.Vote, AccessControlEntry.Allow);
				p.SetBit(Permission.Delete, AccessControlEntry.Deny);
				p.SetBit(Permission.Edit, AccessControlEntry.Deny);
				p.SetBit(Permission.Reply, AccessControlEntry.Allow);
				p.SetBit(Permission.Post, AccessControlEntry.Deny);
				p.SetBit(Permission.View, AccessControlEntry.Allow);
				p.SetBit(Permission.RemoteAttachment, AccessControlEntry.Deny);
			}
			else if (r.Name == Roles.ForumsAdministrator)
			{
				p.SetBit(Permission.SystemAdmin, AccessControlEntry.Deny);
				p.SetBit(Permission.Administer, AccessControlEntry.Allow);
				p.SetBit(Permission.Moderate, AccessControlEntry.Allow);
				p.SetBit(Permission.Vote, AccessControlEntry.Allow);
				p.SetBit(Permission.Delete, AccessControlEntry.Allow);
				p.SetBit(Permission.Edit, AccessControlEntry.Allow);
				p.SetBit(Permission.Reply, AccessControlEntry.Allow);
				p.SetBit(Permission.Post, AccessControlEntry.Allow);
				p.SetBit(Permission.View, AccessControlEntry.Allow);
				p.SetBit(Permission.RemoteAttachment, AccessControlEntry.Allow);
			}
			else if (r.Name == Roles.GalleryAdministrator)
			{
				p.SetBit(Permission.SystemAdmin, AccessControlEntry.Deny);
				p.SetBit(Permission.Administer, AccessControlEntry.Allow);
				p.SetBit(Permission.Moderate, AccessControlEntry.Allow);
				p.SetBit(Permission.Vote, AccessControlEntry.Allow);
				p.SetBit(Permission.Delete, AccessControlEntry.Allow);
				p.SetBit(Permission.Edit, AccessControlEntry.Allow);
				p.SetBit(Permission.Reply, AccessControlEntry.Allow);
				p.SetBit(Permission.Post, AccessControlEntry.Allow);
				p.SetBit(Permission.View, AccessControlEntry.Allow);
				p.SetBit(Permission.RemoteAttachment, AccessControlEntry.Allow);
			}
			else if (r.Name == Roles.Moderator)
			{
				p.SetBit(Permission.SystemAdmin, AccessControlEntry.Deny);
				p.SetBit(Permission.Administer, AccessControlEntry.Deny);
				p.SetBit(Permission.Moderate, AccessControlEntry.Allow);
				p.SetBit(Permission.Vote, AccessControlEntry.Allow);
				p.SetBit(Permission.Delete, AccessControlEntry.Allow);
				p.SetBit(Permission.Edit, AccessControlEntry.Allow);
				p.SetBit(Permission.Reply, AccessControlEntry.Allow);
				p.SetBit(Permission.Post, AccessControlEntry.Allow);
				p.SetBit(Permission.View, AccessControlEntry.Allow);
				p.SetBit(Permission.RemoteAttachment, AccessControlEntry.Allow);
			}
			else if (r.Name == Roles.RegisteredUsers)
			{
				p.SetBit(Permission.SystemAdmin, AccessControlEntry.Deny);
				p.SetBit(Permission.Administer, AccessControlEntry.Deny);
				p.SetBit(Permission.Moderate, AccessControlEntry.Deny);
				p.SetBit(Permission.Vote, AccessControlEntry.Allow);
				p.SetBit(Permission.Delete, AccessControlEntry.Allow);
				p.SetBit(Permission.Edit, AccessControlEntry.Allow);
				p.SetBit(Permission.Reply, AccessControlEntry.Allow);
				p.SetBit(Permission.Post, AccessControlEntry.Deny);
				p.SetBit(Permission.View, AccessControlEntry.Allow);
				p.SetBit(Permission.RemoteAttachment, AccessControlEntry.Deny);
			}
			else if (r.Name == Roles.SystemAdministrator)
			{
				p.SetBit(Permission.SystemAdmin, AccessControlEntry.Allow);
				p.SetBit(Permission.Administer, AccessControlEntry.Allow);
				p.SetBit(Permission.Moderate, AccessControlEntry.Allow);
				p.SetBit(Permission.Vote, AccessControlEntry.Allow);
				p.SetBit(Permission.Delete, AccessControlEntry.Allow);
				p.SetBit(Permission.Edit, AccessControlEntry.Allow);
				p.SetBit(Permission.Reply, AccessControlEntry.Allow);
				p.SetBit(Permission.Post, AccessControlEntry.Allow);
				p.SetBit(Permission.View, AccessControlEntry.Allow);
				p.SetBit(Permission.RemoteAttachment, AccessControlEntry.Allow);
			}
			else if (r.Name == Roles.Owners)
			{
				p.SetBit(Permission.SystemAdmin, AccessControlEntry.Allow);
				p.SetBit(Permission.Administer, AccessControlEntry.Allow);
				p.SetBit(Permission.Moderate, AccessControlEntry.Allow);
				p.SetBit(Permission.Vote, AccessControlEntry.Allow);
				p.SetBit(Permission.Delete, AccessControlEntry.Allow);
				p.SetBit(Permission.Edit, AccessControlEntry.Allow);
				p.SetBit(Permission.Reply, AccessControlEntry.Allow);
				p.SetBit(Permission.Post, AccessControlEntry.Allow);
				p.SetBit(Permission.View, AccessControlEntry.Allow);
				p.SetBit(Permission.RemoteAttachment, AccessControlEntry.Allow);
			}
			else
			{
				p.SetBit(Permission.SystemAdmin, AccessControlEntry.Deny);
				p.SetBit(Permission.Administer, AccessControlEntry.Deny);
				p.SetBit(Permission.Moderate, AccessControlEntry.Deny);
				p.SetBit(Permission.Vote, AccessControlEntry.Deny);
				p.SetBit(Permission.Delete, AccessControlEntry.Deny);
				p.SetBit(Permission.Edit, AccessControlEntry.Deny);
				p.SetBit(Permission.Reply, AccessControlEntry.Deny);
				p.SetBit(Permission.Post, AccessControlEntry.Deny);
				p.SetBit(Permission.View, AccessControlEntry.Deny);
				p.SetBit(Permission.RemoteAttachment, AccessControlEntry.Deny);
			}
		
			SavePermission(p, isNew);
		}


	}
}
